package com.itee.street.dialog;

import com.itee.street.entity.Term;
import com.itee.street.excel.ImportXlsData;
import com.itee.street.excel.ImportXlsxData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.HPos;
import javafx.scene.control.*;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import org.apache.commons.lang.StringUtils;
import org.controlsfx.dialog.Dialogs;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * Created by gonglei on 2015-07-17.
 */
public class ImportFileDialog extends Dialog {

    private static File initialDir;

    private TextField filePathTxt;

    private CheckBox reverseCkb;

    public ImportFileDialog() {
        setHeaderText("导入UID文件");
        ButtonType importBtn = new ButtonType("导入", ButtonBar.ButtonData.OK_DONE);
        GridPane gridPane = new GridPane();
        gridPane.setVgap(8);
        gridPane.setHgap(8);
        gridPane.setMinWidth(450);
        gridPane.setMinHeight(150);
        gridPane.add(new Label("请选择导入文件:"), 0, 0);
        filePathTxt = new TextField();
        gridPane.add(filePathTxt, 1, 0);
        reverseCkb = new CheckBox("UID反向导入");
        gridPane.add(reverseCkb, 1, 1);
        Button choiceBtn = new Button("选择文件");
        gridPane.add(choiceBtn, 2, 0);
        choiceBtn.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Excel文件", "*.xls", "*.xlsx"));
            if (initialDir != null) {
                fileChooser.setInitialDirectory(initialDir);
            }
            File file = fileChooser.showOpenDialog(null);
            if (file != null) {
                initialDir = file.getParentFile();
                filePathTxt.setText(file.getPath());
            }
        });
        ColumnConstraints constraints = new ColumnConstraints();
        constraints.setHalignment(HPos.RIGHT);
        constraints.setPrefWidth(110);
        gridPane.getColumnConstraints().add(constraints);
        ColumnConstraints constraints1 = new ColumnConstraints();
        constraints1.setPrefWidth(220);
        gridPane.getColumnConstraints().add(constraints1);
        getDialogPane().setContent(gridPane);
        getDialogPane().getButtonTypes().add(importBtn);
        final Button btOk = (Button)  getDialogPane().lookupButton(importBtn);
        btOk.addEventFilter(ActionEvent.ACTION, event -> {
            String filePath = filePathTxt.getText();
            if (StringUtils.isNotBlank(filePath)){
                File file = new File(filePathTxt.getText());
                String fileName =file.getName();
                String suffix = fileName.substring(fileName.lastIndexOf("."));
                List<Term> terms=null;
                try {
                    if (suffix.equals(".xls")){
                        terms = ImportXlsData.readExcel(new FileInputStream(file), reverseCkb.isSelected());
                    }else {
                        terms = ImportXlsxData.readExcel(file, reverseCkb.isSelected());
                    }
                    Dialogs.create().masthead(null).title("提示").message("成功导入"+terms.size()+"个").showInformation();
                } catch ( Exception e) {
                    e.printStackTrace();
                    Dialogs.create().masthead(null).title("提示").message("导入失败").showError();
                }
                setResult(terms);
            }
            event.consume();
            close();
        });
    }
}
